package com.molichuxing.service.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.DingTalkUtil;
import com.molichuxing.service.OrderEnterpriseJobService;
import com.molichuxing.services.business.dto.request.modify.EnterpriseRenewCarLeaseModifyBizDto;
import com.molichuxing.services.business.dto.request.modify.EnterpriseRenewLeaseModifyBizDto;
import com.molichuxing.services.business.dto.response.EnterpriseRenewBizDto;
import com.molichuxing.services.business.dto.response.EnterpriseRenewCarBizDto;
import com.molichuxing.services.business.service.OrderEnterpriseBizService;
import com.molichuxing.services.infrastructure.service.IovCarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service("orderEnterpriseJobService")
public class OrderEnterpriseJobServiceImpl implements OrderEnterpriseJobService{
	
	 private static final Logger logger = LoggerFactory.getLogger(OrderEnterpriseJobServiceImpl.class);
	 
	 @Resource
	 private IovCarService iovCarService;
	 
	 @Resource
	 private OrderEnterpriseBizService orderEnterpriseBizService;
	 
	 @Value("${profiles.active.explain}")
	 private String active;
	 
	 @Override
	 public boolean orderEnterpriseRenew() {
		 StringBuilder fail = new StringBuilder();
		 try {
			 List<EnterpriseRenewBizDto> list = orderEnterpriseBizService.getRenewLease(LocalDate.now());
			 if (list == null || list.isEmpty()) {
				 logger.info("[orderEnterpriseRenew]续订单当天({})起租数量为0", LocalDate.now());
				 return true;
			 }
			 for (EnterpriseRenewBizDto enterpriseRenewBizDto : list) {
				 try {
					 modifyRenewLease(enterpriseRenewBizDto);
				 } catch (Exception e) {
					 logger.error("[orderEnterpriseRenew]续订订单({})起租修改失败：({})",
							 enterpriseRenewBizDto.getOrderCode(), e);
					 fail.append(enterpriseRenewBizDto.getOrderCode()).append(",");
				 }
			 }
		 } catch (Exception e) {
			 logger.error("[orderEnterpriseRenew]获取续订单信息失败!", e);
			 DingTalkUtil.exceptionSend(active + "获取续订单信息失败!");
			 return false;
		 } finally {
			 if (fail.length() > 0) {
				 DingTalkUtil.exceptionSend(active + "续订订单起租修改失败，详情见log日志！" +
						 "有以下订单:" + fail.toString());
			 }
		 }

		 return true;
	 }

	/**
	 * 续订订单起租修改
	 *
	 * @param renewBizDto
	 */
	private void modifyRenewLease(EnterpriseRenewBizDto renewBizDto) {
		StringBuilder fail = new StringBuilder();
		List<EnterpriseRenewCarBizDto> carList = renewBizDto.getCarList();
		if (null == carList || carList.isEmpty()) {
			throw new BizException("续订订单(" + renewBizDto.getOrderCode() + ")无续租车辆");
		}
		EnterpriseRenewLeaseModifyBizDto modifyBizDto = new EnterpriseRenewLeaseModifyBizDto();
		modifyBizDto.setOrderCode(renewBizDto.getOrderCode());
		List<EnterpriseRenewCarLeaseModifyBizDto> carModifyList = new ArrayList<>(carList.size());
		try {
			for (EnterpriseRenewCarBizDto carBizDto : carList) {
				Integer odoMileage = null;
				try {
					odoMileage = getOdoMileage(carBizDto.getVin());
					if (null == odoMileage) {
						fail.append("订单车辆(" + carBizDto.getVin() + ")查询里程失败, ");
						continue;
					}
				} catch (Exception e) {
					fail.append("订单车辆(" + carBizDto.getVin() + ")查询里程失败, ");
				}
				EnterpriseRenewCarLeaseModifyBizDto carLeaseModifyBizDto = new EnterpriseRenewCarLeaseModifyBizDto();
				BeanUtils.copyProperties(carBizDto, carLeaseModifyBizDto);
				carLeaseModifyBizDto.setStartMileage(odoMileage);
				carModifyList.add(carLeaseModifyBizDto);
			}
			if (carModifyList.isEmpty()) {
				return;
			}
			modifyBizDto.setCarList(carModifyList);
			orderEnterpriseBizService.modifyRenewLease(modifyBizDto);
		} finally {
			if (fail.length() > 0) {
				throw new BizException(fail.toString());
			}
		}
	}


	/**
	 * 获取车联网实际里程
	 *
	 * @param vin
	 * @return
	 */
	private Integer getOdoMileage(String vin) {
		try {
			return iovCarService.getOdoMileage(vin);
		} catch (Exception e) {
			return null;
		}
	}
}
